package com.ziven.common.widget.setting;

import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.common.widget.R;
import com.ziven.common.widget.value.Background;
import com.ziven.common.widget.value.TakeEffect;
import com.ziven.common.widget.value.TextColor;
import com.ziven.common.widget.value.TextSize;
import com.ziven.common.widget.window.CommonDialog;
import com.ziven.common.widget.value.Image;
import com.ziven.easygo.util.ResourceUtils;

/**
 * @author Ziven
 */
public class DialogPreferenceItem extends BasePreferenceItem {

    private CommonDialog dialog;
    private String title;
    private String message;
    private String positiveButton;
    private String negativeButton;
    private String neutralButton;
    private DialogInterface.OnClickListener clickListener;
    private int styleId = R.style.Common_Widget_Dialog;
    private int gravity = Gravity.BOTTOM;
    private boolean centerButton = true;
    private TakeEffect takeEffect;
    private boolean canceledOnTouchOutside = true;
    private boolean removeLeftRightPadding = true;

    public DialogPreferenceItem(@NonNull CommonDialog dialog) {
        this();
        this.dialog = dialog;
    }

    public DialogPreferenceItem() {
        subIcon = Image.drawableId(R.drawable.drawable_setting_item_right_go);
    }

    public DialogPreferenceItem setStyle(int styleId) {
        this.styleId = styleId;
        return this;
    }

    public DialogPreferenceItem setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public DialogPreferenceItem centerButton(boolean center) {
        centerButton = center;
        return this;
    }

    public DialogPreferenceItem takeEffect(@NonNull TakeEffect effect) {
        takeEffect = effect;
        return this;
    }

    public DialogPreferenceItem setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        this.canceledOnTouchOutside = canceledOnTouchOutside;
        return this;
    }

    public DialogPreferenceItem setRemoveLeftRightPadding(boolean removeLeftRightPadding) {
        this.removeLeftRightPadding = removeLeftRightPadding;
        return this;
    }

    public DialogPreferenceItem setTitle(int titleId) {
        return setTitle(ResourceUtils.getText(titleId));
    }

    public DialogPreferenceItem setTitle(String title) {
        this.title = title;
        return this;
    }

    public DialogPreferenceItem setMessage(int messageId) {
        return setMessage(ResourceUtils.getText(messageId));
    }

    public DialogPreferenceItem setMessage(String message) {
        this.message = message;
        return this;
    }

    public DialogPreferenceItem setListener(DialogInterface.OnClickListener listener) {
        clickListener = listener;
        return this;
    }

    public DialogPreferenceItem setPositiveButton(int titleId) {
        return setPositiveButton(ResourceUtils.getText(titleId));
    }

    public DialogPreferenceItem setPositiveButton(String title) {
        positiveButton = title;
        return this;
    }

    public DialogPreferenceItem setNegativeButton(int titleId) {
        return setNegativeButton(ResourceUtils.getText(titleId));
    }

    public DialogPreferenceItem setNegativeButton(String title) {
        negativeButton = title;
        return this;
    }

    public DialogPreferenceItem setNeutralButton(int titleId) {
        return setNeutralButton(ResourceUtils.getText(titleId));
    }

    public DialogPreferenceItem setNeutralButton(String title) {
        neutralButton = title;
        return this;
    }

    public void showDialog(@NonNull Context context) {
        if(dialog == null) {
            dialog = new CommonDialog(context, styleId)
            .setTitle(title)
            .setMessage(message)
            .setGravity(gravity)
            .centerButton(centerButton)
            .setCanceledOnTouchOutside(canceledOnTouchOutside)
            .setRemoveLeftRightPadding(removeLeftRightPadding)
            .setPositiveButton(positiveButton, clickListener)
            .setNegativeButton(negativeButton, clickListener)
            .setNeutralButton(neutralButton, clickListener);
        }
        if(takeEffect == null) {
            takeEffect = defaultTakeEffect();
        }
        dialog.show(takeEffect);
    }

    public static TakeEffect defaultTakeEffect() {
        return TakeEffect.values(
                CommonDialog.title().add(
                        TextSize.dimenId(R.dimen.common_title_size),
                        TextColor.colorId(R.color.common_title_enable_color)
                ),
                CommonDialog.message().add(
                        TextSize.dimenId(R.dimen.common_item_title_size),
                        TextColor.colorId(R.color.common_sub_title_enable_color)
                ),
                CommonDialog.positive().add(
                        TextSize.dimenId(R.dimen.common_title_size),
                        TextColor.colorId(R.color.common_switch_button_enable_select_color),
                        Background.drawableId(R.drawable.drawable_setting_item_bg_click)
                ),
                CommonDialog.negative().add(
                        TextSize.dimenId(R.dimen.common_title_size),
                        TextColor.colorId(R.color.common_sub_title_enable_color),
                        Background.drawableId(R.drawable.drawable_setting_item_bg_click)
                ),
                CommonDialog.neutral().add(
                        TextSize.dimenId(R.dimen.common_title_size),
                        TextColor.colorId(R.color.common_title_enable_color),
                        Background.drawableId(R.drawable.drawable_setting_item_bg_click)
                )
        );
    }

    @Override
    public boolean onItemClick(BasePreferenceItem data, int position, @NonNull View view) {
        showDialog(view.getContext());
        return true;
    }
}
